package com.atxyj.srb.sms.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atxyj.common.exception.Assert;
import com.atxyj.common.exception.BusinessException;
import com.atxyj.common.result.ResponseEnum;
import com.atxyj.srb.sms.service.SmsService;
import com.atxyj.srb.sms.utils.SmsProperties;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: xyj
 * @date: 2021/7/31
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Override
    public void send(String mobile, String templateCode, Map<String, Object> param) {
        //1、创建远程连接客户端对象
        DefaultProfile profile = DefaultProfile.getProfile(
                SmsProperties.REGION_Id,
                SmsProperties.KEY_ID,
                SmsProperties.KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        //2、创建远程连接的各种参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        //远程服务器地址(下面三个是默认配置)
        request.setSysDomain("dysmsapi.aliyuncs.com");
        //api的版本好
        request.setSysVersion("2017-05-25");
        //当前请求做什么
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", SmsProperties.REGION_Id);

        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", SmsProperties.SIGN_NAME);
        request.putQueryParameter("TemplateCode", templateCode);
        //参数要求以json字符串的形式传输
        Gson gson = new Gson();
        String jsonParam = gson.toJson(param);
        request.putQueryParameter("TemplateParam", jsonParam);
        

        try {
            //使用客户端对象携带请求参数向远程阿里云服务器发起远程调用，并得到响应结果
            CommonResponse response = client.getCommonResponse(request);
            //获取响应结果 由于阿里云短信在某段时间内只能发送有限短信条数 但是前端还是显示为发送成功
            System.out.println("response.getData()：" + response.getData());

            //没有连上阿里云的服务器
            boolean success = response.getHttpResponse().isSuccess();
            Assert.isTrue(success, ResponseEnum.ALIYUN_RESPONSE_ERROR);
            //获取响应结果
            String data = response.getData();
            HashMap<String, String> resultMap = gson.fromJson(data, HashMap.class);
            String code = resultMap.get("code");
            String message = resultMap.get("Message");
             log.info("code :" + code + " message :" + message);
            //业务失败的处理
            Assert.notEquals("isv.BUSINESS_LIMIT_CONTRO", code, ResponseEnum.ALIYUN_SMS_LIMIT_CONTROL_ERROR);
            Assert.equals("OK" , code, ResponseEnum.ALIYUN_SMS_ERROR);
        } catch (ClientException e) {
            //将异常信息写入到服务器日志里面
            log.error("阿里云短信发送sdk调用失败：" + e.getErrCode() + " " + e.getErrMsg());
            //将异常信息返回给前台，打印日志跟踪栈
            throw new BusinessException(ResponseEnum.ALIYUN_SMS_ERROR,e );
        }
    }
}
